//
//  ViewController.m
//  FindSomeone
//
//  Created by 王骊靬 on 3/23/15.
//  Copyright (c) 2015 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain) NSArray *nameArr;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UIButton *stop;
@property (nonatomic, retain) NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.nameArr = @[@"沙长健",
                     @"朱鸿桥",
                     @"黄天宇",
                     @"姜明均",
                     @"王则天",
                     @"洪宝龙",
                     @"于建勋",
                     @"高景旦",
                     @"张明强",
                     @"夏智垚",
                     @"常皓",
                     @"张营营",
                     @"孙嘉胤",
                     @"曹宇",
                     @"杜江",
                     @"马田成",
                     @"王天东",
                     @"郑舒妮",
                     @"张强",
                     @"于成哲",
                     @"王静福",
                     @"包铭礼",
                     @"丁聪",
                     @"赵沙烁",
                     @"马元元",
                     @"胡志朋",
                     @"于游洋",
                     @"李世友",
                     @"杨冰涛"];
    
    NSString *name = [self.nameArr objectAtIndex:arc4random() % self.nameArr.count];

    // 名字label
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 300)];
    [self.nameLabel setText:name];
    [self.nameLabel setTextAlignment:NSTextAlignmentCenter];
    [self.nameLabel setFont:[UIFont systemFontOfSize:100]];
    [self.view addSubview:self.nameLabel];
    
    self.stop = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.stop setFrame:CGRectMake(0, 0, 200, 50)];
    [self.stop setCenter:CGPointMake(self.view.frame.size.width / 2, 480)];
    [self.stop setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.stop.titleLabel setFont:[UIFont systemFontOfSize:40]];
    [self.stop setTitle:@"停止" forState:UIControlStateNormal];
    
    [self.stop setTitle:@"开始" forState:UIControlStateSelected];
    
    [self.stop addTarget:self action:@selector(findSomeone:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:self.stop];
    
    
    
    // 定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeName) userInfo:nil repeats:YES];
}



// 改变名字
- (void)changeName
{
    NSString *name = [self.nameArr objectAtIndex:arc4random() % self.nameArr.count];
    [self.nameLabel setText:name];
}

// 停止
- (void)findSomeone:(UIButton *)button
{
    //[self.nameLabel setText:@"季老板"];
    button.selected = !button.selected;
    if (button.selected) {
        [self.timer invalidate];
        _timer = nil;
    } else {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeName) userInfo:nil repeats:YES];
    }
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
